/*
 * Board.h
 *
 *  Created on: Sep 18, 2012
 *      Author: Larry.Li
 */

#ifndef BOARD_H_
#define BOARD_H_

typedef struct Movement {
	int from;
	int to;
	double score;
} Movement;

typedef struct {
	unsigned long startingBits;
	unsigned long middleBits;
	unsigned long endingBits;
} BitBoard;

typedef struct {
	int player;
	int imbalance;

	int piece_in_square[90];

	BitBoard r_jiang, b_jiang;
	BitBoard r_che, b_che;
	BitBoard r_ma, b_ma;
	BitBoard r_xiang, b_xiang;
	BitBoard r_shi, b_shi;
	BitBoard r_pao, b_pao;
	BitBoard r_bing, b_bing;

	BitBoard r_pieces, b_pieces, all_pieces;
} ChessPosition;

typedef struct {
	BitBoard jiang[90];

	int xiangIndexMapping[90];
	int xiangGoingTopRowMapping[90];
	int xiangGoingBottomRowMapping[90];
	BitBoard xiangGoingTop[14][512];
	BitBoard xiangGoingBottom[14][512];

	BitBoard shi[90];

	BitBoard r_bing[90];
	BitBoard b_bing[90];

	BitBoard cheRank[90][512];
	BitBoard cheFile[90][1024];

	BitBoard paoRank[90][512];
	BitBoard paoFile[90][1024];

	BitBoard maRank[90][512];
	BitBoard maFile[90][1024];

} PieceMovements;

#define INVALID_POS -1

extern BitBoard mask[90];
extern BitBoard rotateMask[90];
extern const unsigned long bitMask[10];
extern const BitBoard fileMask[10];
extern const int indexRowMapping[90];
extern const int indexRotatedRowMapping[90];
extern const int indexRankBitsPositionMapping[90];
extern PieceMovements MOVEMENTS;

extern void initBoard();
extern int genKillingMovements(ChessPosition *chessPos, Movement *movement);
extern int genNoneKillingMovements(ChessPosition *chessPos, Movement *movement);
extern int genJiangEscapeMovements(ChessPosition *chessPos, Movement *movement);

#define NOTHING 0
#define RB 1
#define RS 2
#define RX 3
#define RP 4
#define RM 5
#define RC 6
#define RJ 7

#define BB -1
#define BS -2
#define BX -3
#define BP -4
#define BM -5
#define BC -6
#define BJ -7

#endif /* BOARD_H_ */
